package com.qiaofang.applet.service.member.service.impl;

import com.google.common.collect.Lists;
import com.qiaofang.applet.common.base.MongoBaseService;
import com.qiaofang.applet.common.context.QfContextHolder;
import com.qiaofang.applet.constants.BusinessConstants;
import com.qiaofang.applet.constants.MemberOperateTypeConstants;
import com.qiaofang.applet.constants.MyHouseQueryConstants;
import com.qiaofang.applet.constants.RecordingConstants;
import com.qiaofang.applet.service.member.domain.MemberHouseInfo;
import com.qiaofang.applet.service.member.domain.MemberOperate;
import com.qiaofang.applet.service.member.repository.MemberHouseInfoRepository;
import com.qiaofang.applet.service.member.service.MemberHouseInfoService;
import com.qiaofang.applet.service.other.util.DateTypeUtil;
import com.qiaofang.applet.service.property.service.dto.MyHouseInfoListDto;
import com.qiaofang.applet.service.property.service.dto.MyHouseInfoQueryDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: youqing.yang
 * @Create: 2020/11/20 15:33
 * @Description:
 **/
@Slf4j
@Service
public class MemberHouseInfoServiceImpl extends MongoBaseService<MemberHouseInfo, String> implements MemberHouseInfoService {

    private final MemberHouseInfoRepository repository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    public MemberHouseInfoServiceImpl(MemberHouseInfoRepository repository) {
        super(repository);
        this.repository = repository;
    }

    @Override
    public MyHouseInfoListDto getListedRecording(MyHouseInfoQueryDto myHouseInfoQueryDto) {
        MyHouseInfoListDto myHouseInfoListDto = new MyHouseInfoListDto();

        Query query = new Query();
        Criteria criteria = Criteria.where("corporationCode").is(QfContextHolder.getContext().getCorporationCode());
        criteria.and("propertyUniqueUuid").is(myHouseInfoQueryDto.getPropertyUniqueUuid());

        //获取当前时间
        Date newDate = new Date();
        if (StringUtils.equals(MyHouseQueryConstants.WEEK.getValue(), myHouseInfoQueryDto.getTimeLevel())) {
            Date beginDate = DateTypeUtil.getBeginOrAfterDay(newDate, Calendar.DAY_OF_MONTH, -7);
            criteria.and("createTime").gte(beginDate);
        } else if (StringUtils.equals(MyHouseQueryConstants.MOUTH.getValue(), myHouseInfoQueryDto.getTimeLevel())) {
            Date beginDate = DateTypeUtil.getBeginOrAfterDay(newDate, Calendar.DAY_OF_MONTH, -30);
            criteria.and("createTime").gte(beginDate);
        }
        query.addCriteria(criteria);

        List<MemberHouseInfo> memberHouseInfos = mongoTemplate.find(query, MemberHouseInfo.class);

        Map<String, List<MemberHouseInfo>> recordingTypeMap = Optional.ofNullable(memberHouseInfos).orElseGet(Lists::newArrayList)
                .stream().collect(Collectors.groupingBy(MemberHouseInfo::getRecordingType));

        List<MemberHouseInfo> callList = recordingTypeMap.get(RecordingConstants.CALL.getType());
        List<MemberHouseInfo> chatList = recordingTypeMap.get(RecordingConstants.CHAT.getType());

        myHouseInfoListDto.setCallNum(CollectionUtils.isEmpty(callList) == true ? 0 : callList.size());
        myHouseInfoListDto.setChatNum(CollectionUtils.isEmpty(chatList) == true ? 0 : chatList.size());

        int myHouseViewNum = getMyHouseViewNum(myHouseInfoQueryDto);
        log.info("myHouseViewNum num {}", myHouseViewNum);
        myHouseInfoListDto.setViewsNum(myHouseViewNum);
        return myHouseInfoListDto;
    }

    private int getMyHouseViewNum(MyHouseInfoQueryDto myHouseInfoQueryDto) {

        Criteria criteria = Criteria.where("corporationCode").is(QfContextHolder.getContext().getCorporationCode())
                .and("businessUuid").is(myHouseInfoQueryDto.getPropertyUniqueUuid())
                .and("operateType").is(MemberOperateTypeConstants.EXPLORE.getValue())
                .and("businessType").in(BusinessConstants.SALE_PROPERTY.getCode(), BusinessConstants.RENT_PROPERTY.getCode());

        //获取当前时间
        Date newDate = new Date();
        if (StringUtils.equals(MyHouseQueryConstants.WEEK.getValue(), myHouseInfoQueryDto.getTimeLevel())) {
            Date beginDate = DateTypeUtil.getBeginOrAfterDay(newDate, Calendar.DAY_OF_WEEK, -7);
            criteria.and("createTime").gte(beginDate);
        } else if (StringUtils.equals(MyHouseQueryConstants.MOUTH.getValue(), myHouseInfoQueryDto.getTimeLevel())) {
            Date beginDate = DateTypeUtil.getBeginOrAfterDay(newDate, Calendar.DAY_OF_MONTH, -30);
            criteria.and("createTime").gte(beginDate);
        }
        return (int) mongoTemplate.count(new Query(criteria), MemberOperate.class);
    }
}
